/*
 * key.c
 *
 *  Created on: May 8, 2022
 *      Author: Abyss
 */


#include "config.h"


 //按键下降沿回调
 void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
 {
   UNUSED(GPIO_Pin);

   if(GPIO_Pin == KEY1_Pin)
   {
	   //清空计时时间
	   sys_time = 0;
   }
 }




 //按键上升沿回调
 void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
  UNUSED(GPIO_Pin);

  if(GPIO_Pin == KEY1_Pin)
  {
	  //判断按键按下时间
	  if(sys_time<1000)
	  {
		  printf("KEY_SHORT\r\n");
		  key_status = KEY_SHORT;

	  }
	  else if(sys_time>=1000&&sys_time<=5000)
	  {
		  printf("KEY_CENTER\r\n");
		  key_status = KEY_CENTER;
	  }
	  else
	  {
		  printf("KEY_LONG\r\n");
		  key_status = KEY_LONG;
	  }


		//不同按键状态对应不同操作
		switch(key_status)
		{
		   case KEY_SHORT:
			   key_status = KEY_NULL;
			   if(all_data.power_switch == SET)
			   {
				   HAL_GPIO_WritePin(RELAY_GPIO_Port, RELAY_Pin,RESET);
				   all_data.power_switch = RESET;
				   WS281x_SetRGB(GREEN);
			   }
			   else
			   {
				   HAL_GPIO_WritePin(RELAY_GPIO_Port, RELAY_Pin,SET);
				   all_data.power_switch = SET;
				   WS281x_SetRGB(RED);
			   }

			   break;
		   case KEY_CENTER:

  		        key_status = KEY_NULL;
			    HAL_UART_Transmit(&huart2, "AT+CWQAP\r\n", 10, 1000);
			    printf("rst for softap\r\n");

				  //重启 进行配网
				 HAL_NVIC_SystemReset();

			   break;
		   case KEY_LONG:
			   key_status = KEY_NULL;
			   break;
		   default:;break;
		}

  }
}


